package org.huang.spock.spring.service;

import org.huang.spock.spring.bo.Dept;
import org.huang.spock.spring.bo.Person;
import org.huang.spock.spring.dao.DeptAnnoMapper;
import org.huang.spock.spring.dao.PersonAnnoMapper;
import org.huang.spock.spring.dto.DeptPersonDTO;
import org.huang.spock.spring.param.exception.BizException;
import org.huang.spock.spring.param.QueryDeptPageParam;
import org.huang.spock.spring.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DemoService {
    @Autowired
    private DeptAnnoMapper deptAnnoMapper;
    @Autowired
    private PersonAnnoMapper personAnnoMapper;

    public DeptPersonDTO getByDeptId(Integer id) {
        Dept dept = deptAnnoMapper.getById(id);
        if (Objects.isNull(dept)) {
            throw new BizException(10000, "部门不存在");
        }
        List<Person> personList = personAnnoMapper.listByDeptId(id);
        String now = DateUtil.getNow();
        return new DeptPersonDTO(now, dept, personList);
    }


    public List<Dept> listDeptByCodes(List<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return new ArrayList<>(0);
        }
        //循环调用getone，用于mock多次返回值
        return codes.stream().map(deptAnnoMapper::getOne).collect(Collectors.toList());
    }


    public List<Dept> listDeptPage(QueryDeptPageParam paramPage) {
        return deptAnnoMapper.listPaged(paramPage);
    }


    public DeptPersonDTO queryDeptPerson(Integer deptId,String name,String code,List<Integer> personIds){
        Dept dept = deptAnnoMapper.getParam(deptId,name,code);
        if (Objects.isNull(dept)) {
            throw new BizException(10000, "部门不存在");
        }
        List<Person> personList = personAnnoMapper.listByIds(personIds);
        String now = DateUtil.getNow();
        return new DeptPersonDTO(now, dept, personList);
    }

}
